package com.hczt.xhminiapp.adminapi.conf.security;

import com.hczt.xhminiapp.db.entity.SysUser;
import com.hczt.xhminiapp.db.repository.SysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author 红创-马海强
 * @date 2019-04-01 13:02
 * @说明：oauth用户详情
 */
@Service
public class SysUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserRepository sysUserRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<SysUser> users = sysUserRepository.findByUsername(username);
        if(users.isEmpty()) {
            throw new UsernameNotFoundException(String.format("用户:%s 不存在", username));
        }
        SysUser user = users.get(0);
        return new User(user.getUsername(), user.getPassword(), user.getEnabled(), true, true, true, Collections.emptyList());
    }
}
